using System;
using System.Text;
using System.Xml;
using NetActive.Notifications.Services.WP7.Interfaces;

namespace NetActive.Notifications.Services.WP7
{
    /// <summary>
    /// Abstract base class for Tile, Toast and Raw notifications.
    /// </summary>
    public abstract class NotificationBase : INotification
    {
        protected NotificationBase(Uri clientNotificationChannelUri)
        {
            ClientNotificationChannelUri = clientNotificationChannelUri;

            //set default delivery interval to immediately
            DeliveryInterval = NotificationDeliveryInterval.Immediately;
        }

        /// <summary>
        /// Gets or set the Uri of the notification channel to send this message to.
        /// </summary>
        public Uri ClientNotificationChannelUri { get; set; }

        /// <summary>
        /// Unique message ID, used to track the message after completion.
        /// </summary>
        public Guid? UUID { get; set; }

        /// <summary>
        /// Gets or sets a user state object. This object is returned in all callback events.
        /// </summary>
        public object UserState { get; set; }

        /// <summary>
        /// Internal counter for the number of attempts needed to send this notification.
        /// </summary>
        public int NumberOfAttempts { get; set; }

        /// <summary>
        /// Gets or sets the delivery interval for this notification.
        /// </summary>
        public NotificationDeliveryInterval DeliveryInterval { get; set; }

        /// <summary>
        /// Gets the string representing the target header for this notification type.
        /// </summary>
        public abstract string TargetHeader { get; }

        /// <summary>
        /// Method used to return a byte representation of the notification ready to send to the Push Notification Services.
        /// </summary>
        /// <returns>Byte representation of the notification.</returns>
        public abstract byte[] GetBytes();

        /// <summary>
        /// Returns and integer value representing the selected <seealso cref="INotification.DeliveryInterval"/>.
        /// </summary>
        /// <returns>Integer value representing the delivery interval.</returns>
        public abstract int GetDeliveryInterval();

        #region Internal Helper Methods

        /// <summary>
        /// Method used to return a the settings needed to send the notification to the Push Notification Services.
        /// </summary>
        /// <returns></returns>
        internal static XmlWriterSettings GetSettings()
        {
            return new XmlWriterSettings
            {
                Encoding = new UTF8Encoding(false),
                ConformanceLevel = ConformanceLevel.Document,
                Indent = true
            };
        }

        #endregion
    }
}